package com.dabbo.starship.app.client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import com.dabbo.starship.interfaces.Cell;
import com.dabbo.starship.interfaces.ConsoleMode;

public class ConsoleUI extends JPanel {
	
	private static final long serialVersionUID = 2997883201694130070L;

	private Cell console;
	private BufferedImage 	consoleOnline;
	private BufferedImage 	consoleOffline;
	private Graphics g;
	
	public Point getImageSize() {
		Point po = new Point(consoleOffline.getWidth(), consoleOffline.getHeight() + 20);
		return po;
	}
	
	public ConsoleUI(Cell console) {
		this.console = console;
		try {
			consoleOffline = ImageIO.read(new File("img/ConsoleOffline.png"));
			consoleOnline = ImageIO.read(new File("img/ConsoleOnline.png"));
		} catch (IOException ex) {
			System.out.println("Argh, Image failure.");
		}
	}
	
	protected void paintComponent(Graphics g) {
		this.g = g;
		//this.setSize(imageBackground.getWidth(), imageBackground.getHeight() + 20);
		try {
			switch (console.getCellType()) {
			case Cell.CELL_CONSOLE:
				if (console.isActive()) {
					g.drawImage(consoleOnline,  0, 0, null);
					switch (console.getConsoleMode()) {
					case ConsoleMode.MODE_SELECT_MENU:
						consoleConfiguration();
						break;
					case ConsoleMode.MODE_CARGOBAY:
						consoleCargoBay();
						break;
					default:
						consoleDefault();
						break;
					}				
				} else {
					g.drawImage(consoleOffline,  0, 0, null);				
				}
				break;
			case Cell.CELL_POWER:
				if (console.isActive()) {
					g.drawImage(consoleOnline,  0, 0, null);
				} else {
					g.drawImage(consoleOffline,  0, 0, null);
				}
				powerTerminal();
				break;
			default:
				g.drawImage(consoleOffline,  0, 0, null);				
				writeText("Unknown Terminal Configuration",100,85);
			}
		} catch (RemoteException ex) {
			System.out.println("Remote exception in ConsoleView");
		}
	}
	
	private void consoleCargoBay() throws RemoteException {
		g.setColor(Color.RED);
		writeText("Cargo Bay Management",93,15);

		boolean poweredGrid = false;
		int countMax = 0;
		int countActive = 0;
		Cell[][] roomCells = console.getParent().getCells();
		for (int y = 0; y < roomCells.length; y++) {
			for (int x = 0; x < roomCells[y].length; x++) {
				if (roomCells[y][x].getCellType() == Cell.CELL_CARGO && roomCells[y][x].isActive()) {
					countMax++;
					poweredGrid = true;
					if (roomCells[y][x].getPowerOutput() == -7) {
						countActive++;
					}
				}
			}
		}
		
		if (!poweredGrid) {
			g.setColor(Color.blue);
			g.fillRect(40, 80, 320, 15);
			g.setColor(Color.white);
			writeText("Enable Transporter Grid", 90, 50);
		} else {
			g.setColor(Color.green);
			g.fillRect(40, 80, 320, 15);
			g.setColor(Color.black);
			writeText("Disable Transporter Grid", 85, 50);			
		}
		g.setColor(Color.gray);
		writeText("Active Transporter Pads [ " + countActive + " / " + countMax + " ]", 60, 70);			
		
		g.setColor(Color.gray);
		writeText("Room Power Usage : " + console.getParent().getPowerOutput() + " units", 55, 210);

		defaultConsoleOptions();

	}
	
	private void powerTerminal() throws RemoteException {
		g.setColor(Color.RED);
		writeText("Reator Core Terminal",100,15);
		String mode = "OFFLINE";
		if (console.isActive()) {			
			mode = "ONLINE";
			g.setColor(Color.GREEN);
		}
		// Labels
		writeText("Reactor " + mode, 120,45);
		g.setColor(Color.GRAY);
		writeText("Power Output", 130, 80);
		writeText("Core Power Output : " + console.getPowerOutput() + " units.", 75, 125);
		g.drawRect(50, 130, 300, 15);
		
		// Power bar
		int perc = new Double((295D / 375D) * console.getPowerOutput()).intValue();
		g.setColor(Color.blue);
		g.fillRect(53, 133, perc, 10);
		defaultConsoleOptions();
	}

	private void consoleConfiguration() throws RemoteException {
		g.setColor(Color.RED);
		writeText("Console : Configuration Setup",75,15);

		g.setColor(Color.blue);
		g.fillRect(40, 80, 150, 15);
		g.setColor(Color.white);
		writeText("Unassigned", 45, 50);			

		g.setColor(Color.blue);
		g.fillRect(40, 105, 150, 15);
		g.setColor(Color.white);
		writeText("Captains Console", 30, 75);			

		g.setColor(Color.blue);
		g.fillRect(40, 130, 150, 15);
		g.setColor(Color.white);
		writeText("Helm Console", 38, 100);			

		g.setColor(Color.blue);
		g.fillRect(40, 155, 150, 15);
		g.setColor(Color.white);
		writeText("Tactical Console", 30, 125);			

		g.setColor(Color.blue);
		g.fillRect(40, 180, 150, 15);
		g.setColor(Color.white);
		writeText("Engineering Console", 18, 150);			

		g.setColor(Color.blue);
		g.fillRect(40, 205, 150, 15);
		g.setColor(Color.white);
		writeText("Sensor Console", 35, 175);

		g.setColor(Color.blue);
		g.fillRect(40, 230, 150, 15);
		g.setColor(Color.white);
		writeText("Communications", 30, 200);

		g.setColor(Color.gray);
		g.fillRect(210, 80, 150, 15);
		g.setColor(Color.black);
		writeText("Cargo Bay", 220, 50);

		
		defaultConsoleOptions();
	}
	
	private void defaultConsoleOptions() throws RemoteException {
		g.setColor(Color.gray);
		g.drawLine(40, 260, 360, 260);
		if (console.getParent().isBlockLit()) {
			g.setColor(Color.green);
			g.fillRect(40, 265, 120, 15);
			g.setColor(Color.black);
			writeText("Room Lighting", 20, 235);
		} else {
			g.setColor(Color.red);
			g.fillRect(40, 265, 120, 15);
			g.setColor(Color.white);
			writeText("Room Lighting", 20, 235);			
		}
		if (console.getCellType() == Cell.CELL_CONSOLE && console.getConsoleMode() != ConsoleMode.MODE_SELECT_MENU) {
			g.setColor(Color.gray);
			g.fillRect(260, 265, 100, 15);
			g.setColor(Color.black);
			writeText("Reconfigure", 240, 235);
		}
		
	}
	
	private void consoleDefault() throws RemoteException {
		g.setColor(Color.RED);
		writeText("Console (Unassigned)",100,15);

		g.setColor(Color.gray);
		writeText("Room Power Usage : " + console.getParent().getPowerOutput() + " units", 55, 210);

		defaultConsoleOptions();
	}
		
	private void writeText(String msg, int cx, int cy) {
		g.drawChars(msg.toCharArray(), 0, msg.length(), 35+cx, 42+cy);
	}

}
